using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Entities.UniversalDelegates;
using UnityEngine;

public class UmbrellaSkill4Area : TriggerSkillCollider
{
    public override void Initialize(SkillData data, float radius, bool isShow = true)
    {
        base.Initialize(data, radius, isShow);

        transform.position = owner.transform.position;
        transform.localScale = new Vector3(radius * 0.2f, radius * 0.2f, 1);
        StartCoroutine(ThisSkill(data));
    }

    private IEnumerator ThisSkill(SkillData info)
    {
        var sprite1 = GetComponent<SpriteRenderer>();
        sprite1.color = new Color(1, 1, 1, 0);
        sprite1.DOFade(1, 0.4f);

        yield return new WaitForSeconds(0.35f);


        owner.TakeShield(new FinalDamageInfo
        {
            Source = owner,
            Target = owner,
            skillIdx = 4,
            FinalDamage = owner.Stats[STATS.Power].Value()
        });

        for (int i = 0; i < 5; i++)
        {
            foreach (GameObject obj in targets.ToList())
            {
                if(obj.TryGetComponent(out Damageable tar))
                {
                    owner.ApplyDamage(tar, info, canFeedback);
                }
            }
            yield return new WaitForSeconds(0.5f);
        }

        if (owner.curShield >= 0)
        {

            owner.TakeHeal(new FinalDamageInfo
            {
                Source = owner,
                Target = owner,
                skillIdx = 4,
                FinalDamage = owner.curShield * 0.5f
            });

            owner.TakeShield(new FinalDamageInfo
            {
                Source = owner,
                Target = owner,
                skillIdx = 4,
                FinalDamage = -owner.curShield * 0.5f
            });

            foreach (GameObject obj in targets.ToList())
            {
                if(obj.TryGetComponent(out Damageable tar))
                {
                    SkillData s = info;
                    s.Damage += owner.curShield;
                    owner.ApplyDamage(tar, s, canFeedback);
                    new Buff_Speed(-0.5f, 3).Apply(owner, tar);

                }
            }
        }
        Destroy();
    }
}
